﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 特效包装控件，可以为内容提供指定数量的<see cref="Border"/>用于呈现特效。
    /// </summary>
    public class EffectWrapControl:ContentControl
    {
        public EffectWrapControl()
        {
            this.resetTemplate();
        }
        /// <summary>
        /// 获取或设置允许最大的效果数量
        /// </summary>
        public int MaxCount
        {
            get { return (int)GetValue(MaxCountProperty); }
            set { SetValue(MaxCountProperty, value); }
        }

        public static readonly DependencyProperty MaxCountProperty =
            DependencyProperty.Register("MaxCount", typeof(int), typeof(EffectWrapControl), new PropertyMetadata(6, new PropertyChangedCallback(maxCountChanged)));


        static void maxCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (obj is EffectWrapControl wwec)
            {
                wwec.resetTemplate();
            }
        }

        private void resetTemplate()
        {
            var ct = new ControlTemplate(typeof(EffectWrapControl));
            FrameworkElementFactory f = new FrameworkElementFactory(typeof(Grid), "Grid");
            FrameworkElementFactory p = f;
            for (int i = 0; i < this.MaxCount; i++)
            {
                FrameworkElementFactory b = new FrameworkElementFactory(typeof(Border), $"Border_{i}");
                p.AppendChild(b);
                p = b;
            }


            FrameworkElementFactory cp = new FrameworkElementFactory(typeof(ContentPresenter), $"ContentPresenter");
            p.AppendChild(cp);
            cp.SetBinding(ContentPresenter.ContentProperty, new Binding("Content") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Mode = BindingMode.OneWay });
            ct.VisualTree = f;
            this.Template = ct;
        }

        private Border[] borders;

        /// <summary>
        /// 获取可以用来指定效果的<see cref="Border"/>,最外层<see cref="Border"/>的索引为0
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Border GetBorder(int index)
        {
            return (Border)this.GetTemplateChild($"Border_{index}");
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            List<Border> list = new List<Border>();
            for (int i = 0; i < this.MaxCount; i++)
            {
                list.Add( GetBorder(i));
            }
            borders = list.ToArray();
        }
        /// <summary>
        /// 获取一个尚未指定效果的Border
        /// </summary>
        /// <returns></returns>
        public Border GetFreeBorder()
        {
            if (borders != null)
            {

                foreach (var item in borders)
                {
                    if (item.Effect == null)
                    {
                        return item;
                    }
                }
            }
            return null;
        }
    }
}
